package com.aispeech.asr.comm.enums;

//0:pcm 1:wav 2:ogg_speex 3:ogg_opus 4:mp3 5: opus
public enum EAudioType {
    PCM(0), WAV(1), OGG_SPEEX(2), OGG_OPUS(3), MP3(4), OPUS(5), AAC(6), M4A(7);

    private final int value;

    EAudioType(int value) {
        this.value = value;
    }

    public static int getValueFromKey(String key) {
        for (EAudioType type : EAudioType.values()) {
            if (type.key().equals(key.toLowerCase())) {
                return type.value;
            }
        }

        return 0;
    }

    public static EAudioType fromKey(String key) {
        EAudioType audioType = null;

        if (key == null || key.length() <= 0) return audioType;

        key = key.toLowerCase();

        // 兼容音频类型传递了带有audio/前缀的格式
        int idxAudioPrefix = key.indexOf("audio/");
        if (idxAudioPrefix >= 0) {
            key = key.substring(6);
        }

        if ("ogg".equals(key)) {
            audioType = OGG_SPEEX;
        } else {
            for (EAudioType type : EAudioType.values()) {
                if (type.key().equals(key)) {
                    audioType = type;
                    break;
                }
            }
        }

        return audioType;
    }

    public int getValue() {
        return value;
    }

    public String key() {
        return this.toString().toLowerCase();
    }
}
